import unittest
import requests
import HTMLTestRunner

class TestLogin(unittest.TestCase):
    def setUp(self):
        print('开始')

    def login_user_password(self):
        # 登录验证返回的状态码为 1001
        # 向接口发起请求
        res=requests.get('http://127.0.0.1:8010/api/mall/v1/user/test?mobile=15888555552')
        # 转化为json类型
        res=res.json()
        rescode=1001
        # 采用截断的方式对数据进行对比
        self.assertEquals(rescode,res['code'],res['message'])



    def username_valid(self):
        # 正则验证手机号是否合法
        # 向接口发起请求
        # 登录验证返回的状态码为 1001
        # 向接口发起请求
        res = requests.get('http://127.0.0.1:8010/api/mall/v1/user/test?mobile=15888555552&code=12')
        # 转化为json类型
        res = res.json()
        rescode = 1002
        # 采用截断的方式对数据进行对比
        self.assertEquals(rescode, res['code'], res['message'])

    def tearDown(self):
        print('结束')

if __name__ == '__main__':
    # 创建测试集
    suit=unittest.TestSuite()
    suit.addTests([TestLogin("login_user_password"),TestLogin("username_valid")])

    # runner=HTMLTestRunner.HTMLTestRunner()
    # # unittest.TextTestRunner(verbosity=2).run(suit)
    # runner.run(suit)
    with open('HTMLReport.html', 'wb') as f:
        runner = HTMLTestRunner.HTMLTestRunner(stream=f)
        runner.run(suit)
